import {Component, OnInit, ViewChild} from '@angular/core';
import {ApiUrl} from "../../../../config/api-url";
import {UserInfoModel} from "../../../../models/user-info.model";
import {TreeNodeInterface} from "../../../../shared/interface/TreeNodeInterface";
import {PermissionEditComponent} from "../permission-edit/permission-edit.component";
import {BaseService} from "../../../../shared/service/base.service";
import {LayerService} from "../../../../shared/service/layer.service";
import {Consts} from "../../../../config/consts";
import {Globals} from "../../../../globals";
import {PermissionModel} from "../../../../models/permission.model";
import {ZorroService} from "../../../../shared/service/zorro.service";
import {PermissionPlatformTypeEnum} from "../../../../enums/permission-platform-type.enum";


@Component({
  selector: 'app-permission-list',
  templateUrl: './permission-list.component.html',
  styleUrls: ['./permission-list.component.less']
})
export class PermissionListComponent implements OnInit {

  permissionPlatformTypeEnum = {
    PC_WEB_PAGE : PermissionPlatformTypeEnum.PC_WEB_PAGE,
    MERCHANT_APPLET : PermissionPlatformTypeEnum.MERCHANT_APPLET,
  }

  tabsIndex = 0;
  platformType = this.permissionPlatformTypeEnum.PC_WEB_PAGE;

  isVisible = false;
  userInfo: UserInfoModel;
  isMobile = Globals.isMobile();
  listOfMapData: Array<PermissionModel> = new Array<PermissionModel>();
  mapOfExpandedData: { [key: string]: TreeNodeInterface[] } = {};

  selfPermission: PermissionModel;
  parentPermission = {title: '一级菜单', id: 0};

  @ViewChild('edit', {read: PermissionEditComponent, static: true})
  public edit: PermissionEditComponent;


  constructor(public baseService: BaseService
  ) {

  }

  ngOnInit(): void {
    this.userInfo = JSON.parse(sessionStorage.getItem(Consts.USER_INFO));

    this.findPermissionList();
  }


  addPermission() {
    this.parentPermission = {title: '一级菜单', id: 0};
    this.selfPermission = null;
    this.isVisible = true;
  }

  refresh() {
    this.findPermissionList();
  }

  setVisible(body) {
    console.log("setVisible", body)
    this.isVisible = body.isVisible;
    if (body.refresh) {
      this.findPermissionList();
    } else {
      this.selfPermission = null;
    }
  }

  update(self) {
    this.selfPermission = self;
    if (self.parentId == 0) {
      this.parentPermission = {title: '一级菜单', id: 0};
    } else {
      for (let i = 0; i < this.listOfMapData.length; i++) {
        if (self.parentId == this.listOfMapData[i].id) {
          this.parentPermission = this.listOfMapData[i];
        } else {
          this.listOfMapData[i].children.forEach(item => {
            if (self.parentId == item.id) {
              this.parentPermission = item;
            }
          });
        }
      }
    }
    this.isVisible = true;
  }

  delete(id) {
    this.baseService.layer.modalDeleteConfirm('确定要删除这权限吗？', confirm => {
      if (confirm) {
        this.baseService.delete(ApiUrl.PermissionUrl, id).subscribe(
          data => {
            if (data.status == 0) {
              this.baseService.layer.messageSuccess('删除权限成功！');
              // this.findPermissionList();
              this.edit.parent.getSimpleMenus();
            } else {
              this.baseService.layer.messageError(data.msg);
            }
          }
        );
      }
    });
  }

  noAuthChange(value, permissionId) {
    // console.log(value, permissionId);
    let body = {'noAuth': value, 'id': permissionId};
    this.baseService.put(ApiUrl.PermissionUrl, body).subscribe(
      data => {
        if (data.status == 0) {
          this.baseService.layer.notificationSuccess('修改成功');
        } else {
          this.baseService.layer.notificationError(data.msg);
        }
      }
    );
  }

  navChange(value, permissionId) {
    // console.log(value, permissionId);
    let body = {'nav': value, 'id': permissionId};
    this.baseService.put(ApiUrl.PermissionUrl, body).subscribe(
      data => {
        if (data.status == 0) {
          this.baseService.layer.notificationSuccess('菜单导航设置成功');
        } else {
          this.baseService.layer.notificationError(data.msg);
        }
      }
    );
  }

  openSettingChange(value, permissionId) {
    // console.log(value, permissionId);
    let body = {'openSetting': value ? 1 : 0, 'id': permissionId};
    this.baseService.put(ApiUrl.PermissionUrl, body).subscribe(
      data => {
        if (data.status == 0) {
          this.baseService.layer.notificationSuccess('菜单导航设置成功');
        } else {
          this.baseService.layer.notificationError(data.msg);
        }
      }
    );
  }

  updateSort(sort, permission) {
    // console.log(sort, permission.sort);
    if (sort == permission.sort || sort == '' || sort == null) {
      return;
    }
    let body = {'sort': sort, 'id': permission.id};
    this.baseService.put(ApiUrl.PermissionUrl, body).subscribe(
      data => {
        if (data.status == 0) {
          this.baseService.layer.notificationSuccess('排序更新成功！');
        } else {
          this.baseService.layer.notificationError(data.msg);
        }
      }
    );
  }

  updateTitleSimplify(titleSimplify, permission) {
    if (titleSimplify == '' || titleSimplify == null) {
      return;
    }
    let body = {'titleSimplify': titleSimplify, 'id': permission.id};
    this.baseService.put(ApiUrl.PermissionUrl, body).subscribe(
      data => {
        if (data.status == 0) {
          this.baseService.layer.notificationSuccess('权限缩写名称更新成功！');
        } else {
          this.baseService.layer.notificationError(data.msg);
        }
      }
    );
  }

  addChildren(parent) {
    this.parentPermission = parent;
    this.isVisible = true;
  }


  selectedIndexChange(data){
    this.tabsIndex = data;
    if (this.tabsIndex == 0){
      this.platformType = this.permissionPlatformTypeEnum.PC_WEB_PAGE;
    } else if (this.tabsIndex == 1){
      this.platformType = this.permissionPlatformTypeEnum.MERCHANT_APPLET;
    } else {
      this.baseService.layer.notificationError("tabsIndex非法");
    }
    this.findPermissionList();
  }

  findPermissionList() {
    this.baseService.get(ApiUrl.PermissionUrl + '/list?platformType=' + this.platformType).subscribe(
      data => {
        if (data.status == 0) {
          this.listOfMapData = data.data;
          this.listOfMapData.forEach(item => {
            this.mapOfExpandedData[item.id] = this.baseService.zorro.convertTreeToList(item);
          });
        }
      }
    );
  }

}
